<?php

namespace App\Admin\Controllers;

use App\Models\SysCity;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class SysCityController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header(trans('admin.menu_titles.sys_city'))
//            ->description('description')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header(trans('admin.menu_titles.sys_city'))
//            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header(trans('admin.menu_titles.sys_city'))
//            ->description('description')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header(trans('admin.menu_titles.sys_city'))
//            ->description('description')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new SysCity);
        $grid->model()->orderBy('province_id', 'asc')->orderBy('id', 'asc');
        $grid->model()->orderBy('id', 'desc');
        $grid->id('Id');
        $grid->name(trans('admin.name'));
        $grid->province_name(trans('admin.province'))->display(function () {
            return $this->province->name;
        });
        $grid->city_no(trans('admin.city_no'));
        $grid->full_spell(trans('admin.full_spell'));
        $grid->first_spell(trans('admin.first_spell'));
        $grid->filter(function ($filter) {
            $filter->column(1 / 3, function ($filter) {
                $filter->like('name', trans('admin.city'));
            });
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(SysCity::findOrFail($id));
        $show->id('Id');
        $show->name(trans('admin.name'));
        $show->province_name(trans('admin.province'))->as(function () {
            return $this->province->name;
        });
        $show->city_no(trans('admin.city_no'));
        $show->full_spell(trans('admin.full_spell'));
        $show->first_spell(trans('admin.first_spell'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new SysCity);
        $form->select('province_id', trans('admin.province'))->options(route('admin.api.province'))
            ->rules(['required'])->setLabelClass(['required']);
        $form->text('name', trans('admin.name'))->rules(['required', 'unique:sys_city,name', 'regex:/^[\x80-\xff]+[市]$/'])
            ->setLabelClass(['required'])->placeholder('格式如:XX市');
        $form->text('city_no', trans('admin.city_no'))->rules(['required', 'unique:sys_city,city_no', 'integer'])->setLabelClass(['required']);
        $form->text('full_spell', trans('admin.full_spell'))->rules(['required', 'unique:sys_city,full_spell'])->setLabelClass(['required']);
        $form->text('first_spell', trans('admin.first_spell'))->rules(['required'])->setLabelClass(['required']);

        return $form;
    }
}
